package com.caiju.health.controller;


import com.caiju.health.constant.MessageConstant;
import com.caiju.health.dto.FilesUtils;
import com.caiju.health.dto.ImageUtils;
import com.caiju.health.dto.MonthsAndMemberCountDTO;
import com.caiju.health.dto.WorkCountDTO;
import com.caiju.health.entity.PageResult;
import com.caiju.health.entity.Result;
import com.caiju.health.pojo.CheckItem;
import com.caiju.health.service.ICheackItemService;
import com.caiju.health.service.IMemberService;
import com.caiju.health.vo.QueryPageBean;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/member")
@CrossOrigin    //允许跨域，因为不同端口号，认为不是同一个人
public class MemberController {
    @Autowired
    private ImageUtils imageUtils;
    @Autowired
    private FilesUtils filesUtils;
    @Resource
    private IMemberService memberService;
    @GetMapping("/getMemberReport")
    public Result getMemberReport() throws ParseException {
        MonthsAndMemberCountDTO dto = memberService.getMemberReport();
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,dto);
    }
    //分页查询
    @GetMapping("/findCondition")
    public PageResult findCondition(QueryPageBean queryPageBean){
        //从queryPageBean获取查询的条件
        //currentPage  页码
        Integer currentPage = queryPageBean.getCurrentPage();
        //pageSize 每页显示的条数
        Integer pageSize = queryPageBean.getPageSize();
        //queryString 表示查询的条件
        //根据项目的原型我们发现将来进行搜索的时候是根据项目的编码以及项目名称进行搜索
        String queryString = queryPageBean.getQueryString();
        PageResult pageResult = memberService.findCondition(currentPage,pageSize,queryString);
        return pageResult;
    }

//七流云
    @PostMapping("/image/upload")
    public Result uploadImage(@RequestParam(value = "file",required = false) MultipartFile[] multipartFile){
        if(ObjectUtils.isEmpty(multipartFile)){
            return new Result(true, "请选择图片！！");
        }
        Map<String, List<String>> uploadImagesUrl = imageUtils.uploadImages(multipartFile);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,uploadImagesUrl);
    }

//七牛云 体检上传 member
    @PostMapping("/file/upload")
    public Result uploadFile(@RequestParam(value = "file",required = false) MultipartFile[] multipartFile){
        if(ObjectUtils.isEmpty(multipartFile)){
            return new Result(false, "请选择excel文件！！");
        }
        Map<String, List<String>> uploadFilesUrl = filesUtils.uploadFiles(multipartFile);
//        uploadFilesUrl.get("imageUrl").get(0);
        memberService.uploadFile(uploadFilesUrl.get("imageUrl").get(0));
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,uploadFilesUrl);
    }

    //分页file查询
    @GetMapping("/file/findCondition")
    public PageResult findFileCondition(QueryPageBean queryPageBean){
        //从queryPageBean获取查询的条件
        //currentPage  页码
        Integer currentPage = queryPageBean.getCurrentPage();
        //pageSize 每页显示的条数
        Integer pageSize = queryPageBean.getPageSize();
        //queryString 表示查询的条件
        //根据项目的原型我们发现将来进行搜索的时候是根据项目的编码以及项目名称进行搜索
        String queryString = queryPageBean.getQueryString();
        PageResult pageResult = memberService.findFileCondition(currentPage,pageSize,queryString);
        return pageResult;
    }


    //    删除file数据
    @PostMapping("/file/deleteFile/{id}")
    public Result deleteFile(@PathVariable("id")Integer id){
        System.out.println("========="+id);
        memberService.deleteFile(id);
        //表示需要返回一个Result对象，给前端做响应
        return  new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }




    //工作量统计
    @GetMapping("/getWorkReport")
    public Result getWorkReport()  {
        List<WorkCountDTO> list = memberService.getWorkReport();
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,list);

    }






}
